package com.dream.inspect.modules.app.controller;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dream.inspect.common.utils.PageUtils;
import com.dream.inspect.common.utils.R;
import com.dream.inspect.modules.business.entity.MessageNoticEntity;
import com.dream.inspect.modules.business.service.MessageNoticService;
import com.dream.inspect.modules.business.service.SysMessageService;

/**
 * 消息
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/app/message")
public class AppMessageController {

	@Resource
	private SysMessageService sysMessageService;
	@Resource
	private MessageNoticService messageNoticService;
	
	/**
	 * 消息列表
	 * @param params
	 * @return
	 */
	@RequestMapping("list")
	public R list(@RequestParam Map<String, Object> params){
		if(!params.containsKey("dqbmId")){
			return R.error("请传入当前单位ID").put("data","请传入当前单位ID");
		}
		if(!params.containsKey("dqyhId")){
			return R.error("请传入当前用户ID").put("data","请传入当前用户ID");
		}
		if(!params.containsKey("dqjsId")){
			return R.error("请传入当前角色ID").put("data","请传入当前角色ID");
		}else{
			params.put("dqjsId", ";" + params.get("dqjsId") + ";");
		}
		params.put("deleteFlag", 0);
		PageUtils page = sysMessageService.findMessageListByApp(params);
		return R.ok().put("data", page.getList()).put("count",page.getTotalCount());
	}
	
	@RequestMapping("read")
	public R read(@RequestParam Map<String, Object> params){
		/*if(!params.containsKey("targetId")){
			return R.error("请输入对象ID").put("data","请输入对象ID");
		}*/
		if(!params.containsKey("typeFlag")){
			return R.error("请输入类型标志(1:通知通报;2:指令;3:系统消息;)").put("data","1:通知通报;2:指令;3:系统消息;");
		}
		
		return sysMessageService.read(params);
	}
	
	@RequestMapping("addNotic")
	public R addNotic(MessageNoticEntity notic){
		if(null == notic.getFbDeptId()){
			return R.error("请输入发布单位ID").put("data","请输入发布单位ID");
		}
		notic.setCreateTime(new Date());
		boolean result = messageNoticService.insert(notic);
		if(result){
			return R.ok().put("data", "success");
		}else{
			return R.error("新增通知通报失败").put("data","新增通知通报失败");
		}
	}
}
